package com.lantosec.hmrp.core.mvc;

import com.lantosec.hmrp.core.kit.RequestWrapper;
import com.lantosec.hmrp.core.properties.P;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Configuration;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Enumeration;

/**
 * web总入口
 * 工作：1 将request所有数据进行打印
 * 2 权限控制
 *
 * @author 杨成宇
 * @version 1.0
 * @date 2019/10/22
 */
@javax.servlet.annotation.WebFilter(filterName = "webFilter", urlPatterns = "/*")
@Configuration
@Log4j2
public class WebFilter implements Filter {

    private static final String USER = "login_user";

    public void init(FilterConfig filterConfig) throws ServletException {
        log.info("WebFilter init >>>");
        log.info("filterConfig：" + filterConfig);
    }

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = ((HttpServletRequest) servletRequest);
        //直接过滤swagger-ui，不做处理
        if (StringUtils.indexOf(request.getRequestURI(), "swagger") > -1 || StringUtils.indexOf(request.getRequestURI(), "/v2/api-docs") > -1) {
            filterChain.doFilter(request, servletResponse);
            return;
        }
        log.info(">>>【requestURI】 : " + request.getRequestURI());
        if (P.INFO.getPrintRequestBody()) {
            printHeader(request);
        }
        if (P.INFO.getPrintRequestParameter()) {
            printPara(request);
        }
        if (P.INFO.getPrintRequestBody()) {
            //取Body数据
            RequestWrapper requestWrapper = new RequestWrapper(request);
            String body = requestWrapper.getBody();
            log.info(">>>【requestBodyStr】 : " + body);
            //调用该方法后，表示过滤器经过原来的url请求处理方法
            if (requestWrapper != null) {
                request = requestWrapper;
            }
        }

        //TODO 权限控制处理

        filterChain.doFilter(request, servletResponse);
    }

    public void destroy() {
        log.info("WebFilter destroy ...");
    }

    private void printPara(HttpServletRequest request) {
        Enumeration enu = request.getParameterNames();
        StringBuffer sb = new StringBuffer();
        while (enu.hasMoreElements()) {
            String paraName = (String) enu.nextElement();
            sb.append("[" + paraName + " = " + request.getParameter(paraName) + "]");
        }
        log.info(">>>【requestPara】 : " + sb.toString());
    }

    private void printHeader(HttpServletRequest request) {
        Enumeration enu = request.getHeaderNames();
        StringBuffer sb = new StringBuffer();
        while (enu.hasMoreElements()) {
            String headerName = (String) enu.nextElement();
            sb.append("[" + headerName + " = " + request.getHeader(headerName) + "]");
        }
        log.info(">>>【requestHeader】 : " + sb.toString());
    }
}

